﻿using UnityEngine;
using System.Collections;

public class Block : Character {

	public float jumpForce = GameSystem.jumpForce;
	public float plateformerMovingSpeed = GameSystem.plateformerMovingSpeed;
	private GameObject limitA, limitB;
	public Vector2 movementVector;
	public bool isMovingVertically = true;
	public float moveTime = 1.0f;
	private float moveTimer;

	private enum Position
	{
		PositionA,
		PositionB
	}

	private Position position = Position.PositionA;
	
	protected override void Start ()
	{
		position = Position.PositionA;
		limitA = new GameObject();
		limitA.transform.position = transform.parent.position;
		limitB = new GameObject();
		limitB.transform.position = transform.parent.position + new Vector3(movementVector.x, movementVector.y, 0);
		//animator = this.GetComponent<Animator>();
	}
	
	protected override void Update () {
		moveTimer -= Time.deltaTime;
		if(moveTimer < 0)
			moveTimer = 0;
		Move ();
		Display ();
		
	}

	protected override void OnCollisionEnter2D(Collision2D collision)
	{
		base.state = Character.State.normal;
		Debug.Log("A BASE DE POPOPOPOP ??!");
		if (collision.gameObject.GetComponent<Block> () != null) {
			Debug.Log("A BASE DE POPOPOPOP !");
		}
	}
	
	protected override void OnCollisionExit2D(Collision2D collision)
	{
		//base.state = Character.State.jumping;
	}
	public void Use()
	{
		if(moveTimer > 0)
			return;
		if(position == Position.PositionA) {
			position = Position.PositionB;
			moveToLimit(limitB);
			moveTimer = moveTime;
		} else {
			position = Position.PositionA;
			moveToLimit(limitA);
			moveTimer = moveTime;
		}
		
	}
	protected override void Move()
	{

			
			//bouger jusqu'a bloquage
	}
	
	protected override void Display()
	{
	}

	private void moveToLimit(GameObject limit) {

		iTween.MoveTo(transform.parent.gameObject, iTween.Hash("position", limit.transform.position, "easetype", iTween.EaseType.easeInOutSine, "time", moveTime));
		GameObject.Find("SoundManager").GetComponent<SoundPlayer>().LaunchCoroutSound((int)(Random.Range(0, 3)), 0.2f);
//		if (isMovingVertically) {
//			iTween.MoveTo(transform.parent.gameObject, new Vector3(transform.parent.transform.position.x, limit.transform.position.y), 1);
//		} else {
//			iTween.MoveTo(transform.parent.gameObject, new Vector3(limit.transform.position.x, transform.parent.transform.position.y), 1);
//		}
	}

}
